package com.gzbsoft.sc.modules.app.controller;
import com.gzbsoft.sc.common.config.JwtConfig;
import com.gzbsoft.sc.common.utils.R;
import com.gzbsoft.sc.common.utils.SmsVerification;
import com.gzbsoft.sc.common.validator.ValidatorUtils;
import com.gzbsoft.sc.modules.app.form.LoginForm;
import com.gzbsoft.sc.modules.app.form.RegisterForm;
import com.gzbsoft.sc.modules.app.service.JxUserService;
import com.gzbsoft.sc.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/app")
@Api("APP登录接口")
public class AppLoginController {
    private final JxUserService jxUserService;
    private final JwtConfig jwtConfig;
    private final SysUserService sysUserService;
    public AppLoginController(JxUserService jxUserService, JwtConfig jwtConfig ,SysUserService sysUserService) {
        this.jxUserService = jxUserService;
        this.jwtConfig = jwtConfig;
        this.sysUserService = sysUserService;
    }
    @PostMapping("/login")
    @ApiOperation("登录")
    public R login(@RequestBody LoginForm form) {
        ValidatorUtils.validateEntity(form);
        long userId = jxUserService.login(form);
        String token = jwtConfig.generateToken(userId);
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtConfig.getExpire());
        return R.ok().setObj(map);
    }
    @ApiOperation("退出")
    @PostMapping("/sys/logout")
    public R logout() {//HttpSession session
        //session.removeAttribute("token");
        return R.ok();
    }

    @ApiOperation("注册")
    @PostMapping("/sys/register")
    public R register(@RequestBody RegisterForm form , HttpSession session){
        Date date = new Date();
        long nowTime = date.getTime();
        Object time = session.getAttribute("registerOutTime");
        if(time == null || time.equals(null)){
            return R.error("验证码已过期");
        }else{
            long  dateTime = (long)session.getAttribute("registerOutTime")+(long)60000;
            if(nowTime>dateTime){
                session.removeAttribute("registerOutTime");
                return R.error("验证码已过期");
            }
            Object registerCode =session.getAttribute("registerCode");
            if(!StringUtils.isEmpty(registerCode) && !registerCode.equals(form.getSecurityCode())){
                return R.error("验证码错误");
            }
        }
        ValidatorUtils.validateEntity(form);
        sysUserService.register(form);
        //根据手机号查询用户信息
        return R.ok("注册成功");
    }

    @ApiOperation("校验手机号是否注册")
    @GetMapping("/isRegisterByTel")
    public R isRegisterByTel(@RequestParam String mobile){
        if(mobile == null || mobile == ""){
            return R.error("手机号不能为空");
        }
        return sysUserService.isRegisterByTel(mobile);
    }
    @ApiOperation("发送验证码")
    @GetMapping("/sendRegister")
    public R sendRegister(@RequestParam String tel,HttpSession session) throws  Exception{
        Date date = new Date();
        String registerCode = SmsVerification.UUSix();
        SmsVerification.sendSms(tel,registerCode);
        session.setAttribute("registerOutTime",date.getTime());
        session.setAttribute("registerCode",registerCode);
        return R.ok();
    }
/*    @PostMapping("/register")
    @ApiOperation("注册")
    public R register(@RequestBody RegisterForm form) {
        ValidatorUtils.validateEntity(form);
        jxUserService.register(form);
        return R.ok("注册成功");
    }*/
}
